package templates

import (
	"bytes"
	_ "embed"
	"fmt"
	"html/template"
	"time"

	"gitee.com/Gakusyun/gakusyun-ws/utils"
	"gitee.com/Gakusyun/gakusyun-ws/version"
)

//go:embed welcome.html
var welcomeHTML string

//go:embed about.html
var aboutHTML string

//go:embed stats.html
var statsHTML string

//go:embed dev-login.html
var devLoginHTML string

// TemplateData 模板数据结构
type TemplateData struct {
	VersionString  string
	Uptime         string
	TotalRequests  int
	SuccessCount   int
	NotFoundCount  int
	ErrorCount     int
	SuccessRate    float64
}

// AboutData 关于页面数据结构
type AboutData struct {
	VersionString string
}

// NewTemplateData 创建新的模板数据
func NewTemplateData(stats *utils.ServerStats) *TemplateData {
	uptime := stats.GetUptime().Round(time.Second)

	// 计算成功率
	var successRate float64
	if stats.TotalRequests > 0 {
		successRate = float64(stats.SuccessCount) / float64(stats.TotalRequests) * 100
	}

	return &TemplateData{
		VersionString: version.GetVersionString(),
		Uptime:        uptime.String(),
		TotalRequests: stats.TotalRequests,
		SuccessCount:  stats.SuccessCount,
		NotFoundCount: stats.NotFoundCount,
		ErrorCount:    stats.ErrorCount,
		SuccessRate:   successRate,
	}
}

// NewAboutData 创建关于页面数据
func NewAboutData() *AboutData {
	return &AboutData{
		VersionString: version.GetVersionString(),
	}
}

// RenderWelcome 渲染欢迎页面
func RenderWelcome(stats *utils.ServerStats) (string, error) {
	tmpl, err := template.New("welcome").Parse(welcomeHTML)
	if err != nil {
		return "", fmt.Errorf("failed to parse welcome template: %w", err)
	}

	data := NewTemplateData(stats)
	var buf bytes.Buffer
	if err := tmpl.Execute(&buf, data); err != nil {
		return "", fmt.Errorf("failed to execute welcome template: %w", err)
	}

	return buf.String(), nil
}

// RenderAbout 渲染关于页面
func RenderAbout() (string, error) {
	tmpl, err := template.New("about").Parse(aboutHTML)
	if err != nil {
		return "", fmt.Errorf("failed to parse about template: %w", err)
	}

	data := NewAboutData()

	var buf bytes.Buffer
	if err := tmpl.Execute(&buf, data); err != nil {
		return "", fmt.Errorf("failed to execute about template: %w", err)
	}

	return buf.String(), nil
}

// RenderStats 渲染统计页面
func RenderStats(stats *utils.ServerStats) (string, error) {
	// 创建模板函数映射
	funcMap := template.FuncMap{
		"printf": fmt.Sprintf,
	}

	tmpl, err := template.New("stats").Funcs(funcMap).Parse(statsHTML)
	if err != nil {
		return "", fmt.Errorf("failed to parse stats template: %w", err)
	}

	data := NewTemplateData(stats)
	var buf bytes.Buffer
	if err := tmpl.Execute(&buf, data); err != nil {
		return "", fmt.Errorf("failed to execute stats template: %w", err)
	}

	return buf.String(), nil
}

// RenderDevLogin 渲染开发者登录页面
func RenderDevLogin() (string, error) {
	tmpl, err := template.New("dev-login").Parse(devLoginHTML)
	if err != nil {
		return "", fmt.Errorf("failed to parse dev-login template: %w", err)
	}

	var buf bytes.Buffer
	if err := tmpl.Execute(&buf, nil); err != nil {
		return "", fmt.Errorf("failed to execute dev-login template: %w", err)
	}

	return buf.String(), nil
}